<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="220px">
      <!--手机 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="userInfo.mobile" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <upload-img v-model="userInfo.staffPhoto" />
            <!-- 放置上传图片 -->
            <!-- <upload-img ref="staffPhoto" /> -->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSave">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById, saveUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userInfo: {
        Id: this.$route.params.id,
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getuserInfoById()
  },
  methods: {
    async getuserInfoById() {
      try {
        const res = await getUserDetailById(this.userInfo.Id)
        this.userInfo = res.data
      } catch (error) {
        console.log(error.message)
      }
    },
    async  hSave() {
      const vaild = await this.$refs.userForm.validate()
      if (!vaild) return
      try {
        const res = await saveUserDetailById(this.userInfo)
        this.$message.success(res.message)
        if (this.userInfo.Id === this.$store.getters.userId) {
          this.$store.dispatch('user/getUserInfo')
          this.getuserInfoById()
        }
        this.$router.back()
      } catch (error) {
        console.log(error.message)
      }
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
